package com.example.muchentuner;

import static com.example.muchentuner.ButtonActivity.isAutoModeEnabled;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import com.example.muchentuner.CanvasPainter;

import androidx.annotation.Nullable;

public class TunerView extends View {

    private CanvasPainter canvasPainter;
    private Canvas canvas;
    private PitchDifference pitchDifference;

    public TunerView(Context context) {
        super(context);
        canvasPainter = CanvasPainter.with(getContext());
    }

    public TunerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        canvasPainter = CanvasPainter.with(getContext());
    }

    private boolean openup=false;
    private float back_angle=0;
    private float back_angle2=0;
    private int counter=0;

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvasPainter.paint(pitchDifference).on(canvas);
        this.canvas = canvas;
        if (pitchDifference != null) {
            int abs = Math.abs(getNearestDeviation());//
            boolean shouldDraw = abs <= 60 ||
                    (abs <= 60 * 2 && !isAutoModeEnabled());
            if (shouldDraw) {
                int dev=getNearestDeviation();
                angle = (float)(dev *66 / 60);//先乘以66再除以
                //Log.v("TAG1",String.valueOf(angle));
                //Log.v("TAG2",String.valueOf(back_angle2));

                if(angle!=back_angle)
                {
                    counter=1;
                }
                if(counter==1)
                {
                    back_angle2=back_angle;

                    if(back_angle2>0&&angle<0||back_angle2<0&&angle>0)
                    {
                        anim2();
                    }

                    anim();
                    counter=0;
                }
                drawPointer();

                back_angle=angle;
                }
        }
    }

    public void setPitchDifference(PitchDifference pitchDifference) {
        this.pitchDifference = pitchDifference;
    }

    private void drawDevia(){
        Paint textPaint=new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(40f);
        Paint textPaint2=new Paint();
        textPaint2.setColor(Color.WHITE);
        textPaint2.setTextSize(110f);

        int dev=getNearestDeviation();
        String text2="";
        String text="音准偏差";
        float y=CanvasPainter.y+10;
        if(dev<=60&&dev>=10){
            text2="+"+ String.valueOf(dev);
            canvas.drawText(text2, CanvasPainter.x+255f , y+430f , textPaint2);
        }
        if(dev>0&&dev<10)
        {
            text2="+ "+ String.valueOf(dev);
            canvas.drawText(text2, CanvasPainter.x+265f , y+430f , textPaint2);
        }
        if(dev==0){
            text2="  "+ String.valueOf(dev);
            canvas.drawText(text2, CanvasPainter.x+265f , y+430f , textPaint2);
        }
        if(dev<=-10&&dev>=-60){
            text2="-"+ String.valueOf(Math.abs(dev));
            canvas.drawText(text2, CanvasPainter.x+265f , y+430f , textPaint2);
        }
        if(dev<0&&dev>-10)
        {
            text2="- "+ String.valueOf(Math.abs(dev));
            canvas.drawText(text2, CanvasPainter.x+265f , y+430f , textPaint2);
        }

        canvas.drawText(text, CanvasPainter.x+265f , y+300f , textPaint);
    }

    private void anim(){
        //<动画部分
        ValueAnimator anim = ValueAnimator.ofFloat(0f, angle-back_angle2);
        anim.setDuration(450);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentValue = (float) animation.getAnimatedValue();
                current_angle=currentValue;
                postInvalidate();
            }
        });
        anim.start();

        //动画部分>
    }

    private void anim2(){
        //<动画部分
        ValueAnimator anim = ValueAnimator.ofFloat(back_angle2,0f,angle);
        anim.setDuration(450);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentValue = (float) animation.getAnimatedValue();
                current_angle2=currentValue;
                postInvalidate();
            }
        });
        anim.start();
        //动画部分>
    }

    private float angle;
    private float current_angle;//指针动画
    private float current_angle2;//进度条动画

    private void drawPointer(){
        //Log.v("TAG",String.valueOf(dev));
        //Log.v("TAG2",String.valueOf(back_angle));

        Paint paint6=new Paint();
        paint6.setAntiAlias(true);
        paint6.setColor(Color.WHITE);
        Paint paintLoadArc = new Paint();
        paintLoadArc.setAntiAlias(true);
        paintLoadArc.setStrokeWidth(80f);
        paintLoadArc.setStyle(Paint.Style.STROKE);
        paintLoadArc.setColor(Color.WHITE);

        float radium=10;
        float length=420;

        float offset=10;
        float x=CanvasPainter.x;
        float y=CanvasPainter.y+offset;
        Path path = new Path();
        path.moveTo(x+radium,y);
        path.lineTo(x,y-length);
        path.lineTo(x-radium,y);
        RectF circle=new RectF(x-radium,y-radium,x+radium,y+radium);
        path.addArc(circle,0,180);


        //current_angle=Math.round(current_angle);
        Matrix matrix=new Matrix();
        matrix.postRotate(back_angle2,x,y);
        path.transform(matrix);
        matrix.reset();
        matrix.postRotate(current_angle,x,y);
        path.transform(matrix);

        canvas.drawPath(path,paint6);

        float width=(float)(CanvasPainter.gaugeWidth*0.8);
        float rectfp1x=x-width;
        float rectfp1y=y-width;
        float rectfp2x=x+width;
        float rectfp2y=y+width;
        float startAngle=270;
        RectF LoadArc_Rec = new RectF(rectfp1x,rectfp1y,rectfp2x,rectfp2y);

        if(back_angle2>0&&angle<0||back_angle2<0&&angle>0)
        {
            canvas.drawArc(LoadArc_Rec,startAngle,current_angle2,false,paintLoadArc);
        }
        else if(back_angle2>0&&angle>0||back_angle2<0&&angle<0||back_angle2==0)
        {
            canvas.drawArc(LoadArc_Rec,startAngle,back_angle2+current_angle,false,paintLoadArc);//终于明白了，是要用back_angle2加上current_angle!!!
        }

        drawDevia();

    }

    private int getNearestDeviation() {
        float deviation = (float) pitchDifference.deviation;
        int rounded = Math.round(deviation);
        //return Math.round(rounded / 10f) * 10;
        return rounded;
    }

}
